﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.SceneManagement;
public class SceneLoadManager : SceneBase
{
    //异步对象
    public Text loadingText;
    public Slider loadingSlider;
    //读取场景的进度，它的取值范围在0 - 1 之间。
    //显示进度的文本
    //进度条的数值
     float progressValue;
     float sliderNum;
    //进度条
    [Tooltip("下个场景的名字")]

     AsyncOperation async = null;

    public bool isStartLoad;
    void Start()
    {
        if (SceneDataManager.sceneName != "LoadingScene")
        {
            isStartLoad = true;
            StartCoroutine("LoadScene");
        }
        else
        {
            StartCoroutine("LoadScene");
        }
    }
    public IEnumerator LoadScene()
    {
        async = SceneManager.LoadSceneAsync(SceneDataManager.sceneName);
        async.allowSceneActivation = false;
        while (!async.isDone)
        {
            if (async.progress < 0.9f)
                progressValue = async.progress;
            else
                progressValue = 1.0f;
            if (sliderNum <= progressValue)
                sliderNum += Time.deltaTime;
            loadingSlider.value = sliderNum;
            loadingText.text = (int)(loadingSlider.value * 100) + " %";

            if (loadingSlider.value>0.9&& progressValue > 0.9)
            {
                if (isStartLoad)
                    async.allowSceneActivation = true;
            }

            yield return null;
        }

    }
    public void StartLoad(string str)
    {
        isStartLoad = true;
        SceneDataManager.sceneName = str;
    }
}